﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SonBigSum.Question;
namespace SonBigSum.Answer
{
    class Albin: ISonBigSum
    {
        public int SonBigSum(int[] arr)
        {
            int len = arr.Length;
            int semi = int.MinValue;
            int semi1 = int.MinValue;
            int[] flag = new int[len];
            int sum = arr[0];
            int result = int.MinValue;
            int result1 = int.MinValue;
            if (len == 1)
            {
                return sum;
            }
            flag[0] = (sum > 0) ? 1 : -1;
            for (int i = 1; i < len; i++)
            {
                if (arr[i] > 0)
                {
                    flag[i] = 1;
                    sum = (sum <= 0) ? arr[i] : sum + arr[i];
                }
                else
                {
                    flag[i] = -1;
                    if (sum <= 0)
                    {
                        sum = (arr[i] > sum) ? arr[i] : sum;
                    }
                    else
                    {
                        semi = sum;
                        sum += arr[i];
                        if (flag[i] * flag[i - 1] == -1)
                        {
                            if (arr[i - 1] > sum)
                            {
                                sum = arr[i - 1];
                                semi = sum;
                            }
                        }
                        else
                        {
                            sum = (sum < 0) ? int.MinValue : sum;
                        }
                    }
                }
                int[] arr1 = { semi, sum, result };
                result = Big(arr1);
            }
            sum = arr[len - 1];
            flag[len - 1] = (sum > 0) ? 1 : -1;
            for (int i = len - 2; i >= 0; i--)
            {
                if (arr[i] > 0)
                {
                    flag[i] = 1;
                    sum = (sum <= 0) ? arr[i] : sum + arr[i];
                }
                else
                {
                    flag[i] = -1;
                    if (sum <= 0)
                    {
                        sum = (arr[i] > sum) ? arr[i] : sum;
                    }
                    else
                    {
                        semi1 = sum;
                        sum += arr[i];
                        if (flag[i] * flag[i + 1] == -1)
                        {
                            if (arr[i + 1] > sum)
                            {
                                sum = arr[i + 1];
                                semi1 = sum;
                            }
                        }
                        else
                        {
                            sum = (sum < 0) ? int.MinValue : sum;
                        }
                    }
                }
                int[] arr2 = { semi1, sum, result1 };
                result1 = Big(arr2);
            }
            result = (result > result1) ? result : result1;
            return result;
        }
        public int Big(int[] arr)
        {
            int sum = int.MinValue;
            for (int i = 0; i < arr.Length; i++)
            {
                sum = (sum < arr[i]) ? arr[i] : sum;
            }
            return sum;
        }
    }
}
